package com.whmskj.xjlhsc.pc.productionreport.task.month;

import com.whmskj.xjlhsc.pc.productionreport.task.mapper.ProductionMapper;
import com.whmskj.xjlhsc.pc.productionreport.task.pojo.DayVO;
import com.whmskj.xjlhsc.pc.productionreport.task.pojo.MonthVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-09-16 17:45
 */

@Slf4j
@Component
@EnableScheduling
public class MonthTask {
    @Autowired
    private ProductionMapper productionMapper;
    //每月21号早上10点执行
    @Scheduled(cron = "0 0 10 21 * ?")
    //每隔5秒执行
//    @Scheduled(cron = "0/5 * * * * ?")
    public void monthTask(){
        List<MonthVO> monthProductionData = productionMapper.getMonthProductionData();
        for (MonthVO monthProductionDatum : monthProductionData) {
            //所有小数四舍五入保留两位小数
            monthProductionDatum.setAClassProductionKg(Math.round(monthProductionDatum.getAClassProductionKg() * 100.0) / 100.0f);
            monthProductionDatum.setAClassProductionKm(Math.round(monthProductionDatum.getAClassProductionKm() * 100.0) / 100.0f);
            monthProductionDatum.setAClassEfficiency(Math.round(monthProductionDatum.getAClassEfficiency() * 100.0) / 100.0f);
            monthProductionDatum.setBClassProductionKg(Math.round(monthProductionDatum.getBClassProductionKg() * 100.0) / 100.0f);
            monthProductionDatum.setBClassProductionKm(Math.round(monthProductionDatum.getBClassProductionKm() * 100.0) / 100.0f);
            monthProductionDatum.setBClassEfficiency(Math.round(monthProductionDatum.getBClassEfficiency() * 100.0) / 100.0f);
            monthProductionDatum.setCClassProductionKg(Math.round(monthProductionDatum.getCClassProductionKg() * 100.0) / 100.0f);
            monthProductionDatum.setCClassProductionKm(Math.round(monthProductionDatum.getCClassProductionKm() * 100.0) / 100.0f);
            monthProductionDatum.setCClassEfficiency(Math.round(monthProductionDatum.getCClassEfficiency() * 100.0) / 100.0f);
            monthProductionDatum.setTotalProductionKg(Math.round(monthProductionDatum.getTotalProductionKg() * 100.0) / 100.0f);
            monthProductionDatum.setTotalProductionKm(Math.round(monthProductionDatum.getTotalProductionKm() * 100.0) / 100.0f);
            monthProductionDatum.setTotalEfficiency(Math.round(monthProductionDatum.getTotalEfficiency() * 100.0) / 100.0f);
        }
        log.info("monthProductionData:{}",monthProductionData);
        //插入数据库
        //获取当前月份，要2025-09-01，日期部分拼接-01
        LocalDate now = LocalDate.now();
        String month = now.getYear() + "-" + now.getMonthValue() + "-01";
        if (productionMapper.getDayProductionDataByMonthTask(month) == 0) {
            productionMapper.insertMonthProductionData(monthProductionData);
        }
    }
}
